import redis
import re

class RedisHelper(object):
    def __init__(self):
        self.conn = redis.Redis(host='127.0.0.1', port=6379)

    def set(self, name, value):
        self.conn.hset('proxy_ips', name, value)

    def get(self, name):
        value = self.conn.hget('proxy_ips', name)
        if value is not None:
            value = eval(value)
        return value

    def getkeys(self):
        value = self.conn.hkeys('proxy_ips')
        return value

    def getall(self):
        value = self.conn.hvals('proxy_ips')
        return value

    def delete(self, name):
        self.conn.hdel('proxy_ips', name)

    def deleteall(self):
        keys = self.getkeys()
        if len(keys) > 0:
            self.conn.hdel('proxy_ips', *keys)

    def temp_set(self, name, value):
        matchobj = re.search(r'\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}', name, re.M | re.I)
        if matchobj is not None:
            self.conn.hset('proxy_temp', name, value)

    def temp_get(self, name):
        value = self.conn.hget('proxy_temp', name)
        if value is not None:
            value = eval(value)
        return value

    def temp_getkeys(self):
        keys = self.conn.hkeys('proxy_temp')
        return keys

    def temp_getall(self):
        value = self.conn.hgetall('proxy_temp')
        return value

    def temp_delete(self, name):
        self.conn.hdel('proxy_temp', name)

    def temp_deleteall(self):
        keys = self.temp_getkeys()
        self.conn.hdel('proxy_temp', *keys)


if __name__ == "__main__":
    redis = RedisHelper()
    print(redis.temp_getkeys())
